home *** CD-ROM | disk | FTP | other *** search
/ MacHack 1994 / MacHack 1994.toast / MacHack™ 1987-1994 / MacHack™ '90 / MacHack'90 Proceedings / John Norstad / Reusable Code / Source / prog.c < prev    next >
Encoding:
C/C++ Source or Header  |  1990-06-10  |  14.0 KB  |  596 lines  |  [TEXT/MPS ]

  1. /*______________________________________________________________________
  2.  
  3.     Sample - Disinfectant Source Code Sample Program.
  4.     
  5.     Version 2.0b1.
  6.  
  7.     John Norstad
  8.     Academic Computing and Network Services
  9.     Northwestern University
  10.     2129 Sheridan Road
  11.     Evanston, IL 60208
  12.  
  13.     Bitnet: jln@nuacc
  14.     Internet: jln@acns.nwu.edu
  15.     AppleLink: a0173
  16.     CompuServe: 76666,573
  17.     
  18.     Copyright © 1988, 1989, 1990 Northwestern University.  Permission is 
  19.     granted to use this code in your own projects, provided you give credit 
  20.     to both John Norstad and Northwestern University in your about box or 
  21.     document.
  22. _____________________________________________________________________*/
  23.  
  24.  
  25. /*______________________________________________________________________
  26.     
  27.     prog.c - Disinfectant Main Module.
  28.  
  29.     This is the main module for Disinfectant.  It contains the basic
  30.     main event loop processing.
  31. _____________________________________________________________________*/
  32.  
  33.  
  34. #pragma load "precompile"
  35. #include "utl.h"
  36. #include "rez.h"
  37. #include "glob.h"
  38. #include "wstm.h"
  39. #include "abou.h"
  40. #include "pref.h"
  41. #include "help.h"
  42. #include "main.h"
  43. #include "misc.h"
  44. #include "vscn.h"
  45. #include "init.h"
  46. #include "prog.h"
  47.  
  48. #pragma segment prog
  49.  
  50. extern void _DataInit();
  51.  
  52. /*_____________________________________________________________________
  53.  
  54.     Global Variables.
  55. _____________________________________________________________________*/
  56.  
  57.  
  58. static WindowPtr            Front;                /* ptr to front window */
  59. static WindowObject        *FrontObj;            /* ptr to front window object */
  60. static WindKind            FrontKind;            /* front window kind */
  61.  
  62. /*_____________________________________________________________________
  63.  
  64.     Command - Process a Command.
  65.     
  66.     Entry:        mResult = 16/menu number, 16/item number.
  67. _____________________________________________________________________*/
  68.  
  69.  
  70. static void Command (long mResult)
  71.  
  72. {
  73.     short                 theMenu;                /* menu number */
  74.     short                    theItem;                /* item number */
  75.     Str255                daName;                /* desk accessory name */
  76.     GrafPtr                 savePort;            /* saved grafport */
  77.  
  78.     /* Extact the item and menu numbers. */
  79.  
  80.     theItem = mResult & 0xffff;
  81.     theMenu = (mResult >> 16) & 0xffff;
  82.     
  83.     /* Check for and process help mode. */
  84.     
  85.     if (HelpMode) {
  86.         if (theMenu) 
  87.             help_Open(tagCmdBase + tagCmdMult*(theMenu-appleMID) + theItem);
  88.         HiliteMenu(0);
  89.         return;
  90.     };
  91.  
  92.     switch (theMenu) {
  93.     
  94.         /* Process the command. */
  95.     
  96.         case appleMID:
  97.         
  98.             if (theItem == aboutCommand) {
  99.                 abou_Open();
  100.             } else if (theItem == helpCommand) {
  101.                 help_Open(0);
  102.             } else {
  103.                 GetItem(GetMHandle(appleMID), theItem, daName);
  104.                 GetPort(&savePort);
  105.                 (void) OpenDeskAcc(daName);
  106.                 SetPort(savePort);
  107.             };
  108.             break;
  109.  
  110.         case fileMID:
  111.         
  112.             switch (theItem) {
  113.                 case closeCommand:
  114.                     if (FrontKind == daWind) {
  115.                         CloseDeskAcc(((WindowPeek)Front)->windowKind);
  116.                     } else {
  117.                         if (FrontObj->close) (*FrontObj->close)();
  118.                     };
  119.                     break;
  120.                 case saveAsCommand:
  121.                     if (FrontObj->save) (*FrontObj->save)();
  122.                     break;
  123.                 case pageSetupCommand:
  124.                     if (FrontObj->pageSetup) {
  125.                         PrOpen();
  126.                         (*FrontObj->pageSetup)();
  127.                         PrClose();
  128.                     };
  129.                     break;
  130.                 case printCommand:
  131.                     if (FrontObj->print) {
  132.                         PrOpen();
  133.                         misc_PrintError((*FrontObj->print)(false));
  134.                         PrClose();
  135.                     };
  136.                     break;
  137.                 case printOneCommand:
  138.                     if (FrontObj->print) {
  139.                         PrOpen();
  140.                         misc_PrintError((*FrontObj->print)(true));
  141.                         PrClose();
  142.                     };
  143.                     break;
  144.                 case prefsCommand:
  145.                     pref_Open();
  146.                     break;
  147.                 case quitCommand:
  148.                     Done = true;
  149.                     break;
  150.                 default:
  151.                     break;
  152.             }
  153.             break;
  154.             
  155.         case editMID:
  156.             if (!SystemEdit(theItem-1) && FrontObj->edit) 
  157.                 (*FrontObj->edit)(theItem-1);
  158.             break;
  159.             
  160.         case scanMID:
  161.         case disinfectMID:
  162.         
  163.             main_DoScan(theMenu, theItem);
  164.             break;
  165.             
  166.         case protectMID:
  167.             switch (theItem) {
  168.                 case installCommand:
  169.                     misc_InstallINIT();
  170.                     break;
  171.                 case extractCommand:
  172.                     misc_ExtractINIT();
  173.                     break;
  174.             };
  175.             break;
  176.  
  177.         default:
  178.             break;
  179.  
  180.     }; /* switch */
  181.     
  182.     /* Turn off the menu hiliting and return. */
  183.  
  184.     HiliteMenu(0);
  185. }
  186.     
  187. /*______________________________________________________________________
  188.  
  189.     Adjust - Adjust Menus.
  190. _____________________________________________________________________*/
  191.  
  192.  
  193. static void Adjust (void)
  194.  
  195. {
  196.     static WindKind        oldFrontKind = mainWind;    
  197.                                                             /* previous front window type */
  198.     static Boolean            oldScanning = false;    /* previous scanning context */
  199.     static Boolean            oldHelpMode = false;    /* previous help mode */
  200.     MenuHandle                fileM;                    /* handle to file menu */
  201.     MenuHandle                editM;                    /* handle to edit menu */
  202.     MenuHandle                scanM;                    /* handle to scan menu */
  203.     MenuHandle                disinfectM;                /* handle to disinfect menu */
  204.     
  205.     fileM = GetMHandle(fileMID);
  206.     editM = GetMHandle(editMID);
  207.     scanM = GetMHandle(scanMID);
  208.     disinfectM = GetMHandle(disinfectMID);
  209.     
  210.     FrontKind = misc_GetWindKind(FrontWindow());
  211.     
  212.     if (HelpMode) {
  213.         if (oldHelpMode) return;
  214.         EnableItem(fileM, closeCommand);
  215.         EnableItem(fileM, saveAsCommand);
  216.         EnableItem(fileM, pageSetupCommand);
  217.         EnableItem(fileM, printCommand);
  218.         EnableItem(fileM, printOneCommand);
  219.         EnableItem(fileM, quitCommand);
  220.         EnableItem(editM, undoCommand);
  221.         EnableItem(editM, cutCommand);
  222.         EnableItem(editM, copyCommand);
  223.         EnableItem(editM, pasteCommand);
  224.         EnableItem(editM, clearCommand);
  225.         EnableItem(editM, 0);
  226.         EnableItem(scanM, 0);
  227.         EnableItem(disinfectM, 0);
  228.     } else {
  229.         if (oldFrontKind == FrontKind  && oldScanning == Scanning &&
  230.             oldHelpMode == HelpMode) return;
  231.         if (FrontKind == daWind) {
  232.             EnableItem(fileM, closeCommand);
  233.             DisableItem(fileM, saveAsCommand);
  234.             DisableItem(fileM, pageSetupCommand);
  235.             DisableItem(fileM, printCommand);
  236.             DisableItem(fileM, printOneCommand);
  237.             if (Scanning) {
  238.                 DisableItem(fileM, quitCommand);
  239.                 DisableItem(scanM, 0);
  240.                 DisableItem(disinfectM, 0);
  241.             } else {
  242.                 EnableItem(fileM, quitCommand);
  243.                 EnableItem(scanM, 0);
  244.                 EnableItem(disinfectM, 0);
  245.             };
  246.             EnableItem(fileM, 0);
  247.             EnableItem(editM, undoCommand);
  248.             EnableItem(editM, cutCommand);
  249.             EnableItem(editM, copyCommand);
  250.             EnableItem(editM, pasteCommand);
  251.             EnableItem(editM, clearCommand);
  252.             EnableItem(editM, 0);
  253.         } else{
  254.             if (FrontObj->adjust) (*FrontObj->adjust)();
  255.         };
  256.     };
  257.     DrawMenuBar();
  258.     oldFrontKind = FrontKind;
  259.     oldScanning = Scanning;
  260.     oldHelpMode = HelpMode;
  261. }
  262.  
  263. /*_____________________________________________________________________
  264.  
  265.     prog_Event - Fetch and Handle the Next Event.
  266.                 
  267.     This routine should be called to do all non-modal event processing.
  268.     It handles suspend, resume, and update events properly.  It also
  269.     keeps the initial integrity checksum going.
  270. _____________________________________________________________________*/
  271.  
  272.  
  273. void prog_Event (void)
  274.  
  275. {
  276.     EventRecord        event;            /* event */
  277.     short                mask;                /* event mask */
  278.     WindowPtr        whichWindow;    /* ptr to window clicked in */
  279.     WindowObject    *whichObj;        /* ptr to object for window clicked in */
  280.     short                partCode;        /* window part code */
  281.     short                key;                /* ascii code of key pressed */
  282.     unsigned long    newSize;            /* new window size */
  283.     long                sleep;            /* sleep time */
  284.     Boolean            eventAvail;        /* WaitNextEvent function result */
  285.     Point                where;            /* location of mouse down */
  286.     GrafPtr            savedPort;        /* saved grafport */
  287.     short                item;                /* dialog item */
  288.     
  289.     /* Save the current grafport. */
  290.     
  291.     GetPort(&savedPort);
  292.     
  293.     /* Get the front window and a pointer to its object. */
  294.     
  295.     Front = FrontWindow();
  296.     FrontObj = (WindowObject*)((WindowPeek)Front)->refCon;
  297.  
  298.     /* Adjust menus. */
  299.  
  300.     Adjust();
  301.  
  302.     /* Take care of any periodic tasks for all open windows. */
  303.     
  304.     whichWindow = FrontWindow();
  305.     while (whichWindow) {
  306.         if (!utl_IsDAWindow(whichWindow)) {
  307.             whichObj = (WindowObject*)((WindowPeek)whichWindow)->refCon;
  308.             if (whichObj->periodic) {
  309.                 SetPort(whichWindow);
  310.                 (*whichObj->periodic)();
  311.             };
  312.         };
  313.         whichWindow = (WindowPtr)((WindowPeek)whichWindow)->nextWindow;
  314.     };
  315.     SetPort(savedPort);
  316.         
  317.     /* Initialize the event mask and the sleep time. */
  318.     
  319.     mask = everyEvent;
  320.     if ((Scanning && !FloppyWait) || !InForeground) mask ^= diskMask;
  321.     sleep = (Scanning || FrontKind == prefWind ||
  322.         abou_IsOpen()) ? 0 : LongSleep;
  323.         
  324.     /* Get the next event, if any. */
  325.     
  326.     eventAvail = utl_WaitNextEvent(mask, &event, sleep, nil);
  327.     
  328.     /* Check for and process dialog event. */
  329.     
  330.     if (IsDialogEvent(&event) && event.what != osEvt && 
  331.         event.what != diskEvt &&
  332.         (event.what != keyDown || !(event.modifiers&cmdKey)) &&
  333.         (event.what != mouseDown || !HelpMode)) {
  334.         if (event.what != keyDown || !FrontObj->dialogPre ||
  335.             (*FrontObj->dialogPre)(event.message & charCodeMask)) {
  336.             if (DialogSelect(&event, &whichWindow, &item)) {
  337.                 whichObj = (WindowObject*)((WindowPeek)whichWindow)->refCon;
  338.                 if (whichObj->dialogPost) {
  339.                     SetPort(whichWindow);
  340.                     (*whichObj->dialogPost)(item);
  341.                 };
  342.             };
  343.         };
  344.         SetPort(savedPort);
  345.         return;
  346.     };
  347.     
  348.     /* Return if there's no event to handle. */
  349.     
  350.     if (!eventAvail) return;
  351.     
  352.     /* Process the event. */
  353.     
  354.     switch (event.what) {
  355.     
  356.         case mouseDown:
  357.             
  358.             partCode = FindWindow(event.where, &whichWindow);
  359.             if (whichWindow && !utl_IsDAWindow(whichWindow)) 
  360.                 whichObj = (WindowObject*)((WindowPeek)whichWindow)->refCon;
  361.         
  362.             switch (partCode) {
  363.                 
  364.                 case inSysWindow:
  365.                 
  366.                     SystemClick(&event, whichWindow);
  367.                     break;
  368.                     
  369.                 case inMenuBar:
  370.                 
  371.                     MenuPick = true;
  372.                     Command(MenuSelect(event.where));
  373.                     break;
  374.                     
  375.                 case inGoAway:
  376.                 
  377.                     if (TrackGoAway(whichWindow, event.where))
  378.                         if (whichObj->close) (*whichObj->close)();
  379.                     break;
  380.                     
  381.                 case inDrag:
  382.                 
  383.                     DragWindow(whichWindow, event.where, &DragRect);
  384.                     whichObj->moved = true;
  385.                     break;
  386.                     
  387.                 case inGrow:
  388.                 
  389.                     SetPort(whichWindow);
  390.                     newSize = GrowWindow(whichWindow, event.where, 
  391.                         &whichObj->sizeRect);
  392.                     if (newSize && whichObj->grow) 
  393.                         (*whichObj->grow)(newSize >> 16, newSize & 0xffff);
  394.                     whichObj->moved = true;
  395.                     break;
  396.                     
  397.                 case inZoomIn:
  398.                 case inZoomOut:
  399.                 
  400.                     if (TrackBox(whichWindow, event.where, partCode)) {
  401.                         SetPort(whichWindow);
  402.                         if (partCode == inZoomOut) wstm_ComputeStd(whichWindow);
  403.                         EraseRect(&whichWindow->portRect);
  404.                         ZoomWindow(whichWindow, partCode, false);
  405.                         if (whichObj->zoom) (*whichObj->zoom)();
  406.                         whichObj->moved = true;
  407.                     };
  408.                     break;
  409.                     
  410.                 case inContent:
  411.                 
  412.                     if (whichWindow != FrontWindow()) {
  413.                         SelectWindow(whichWindow);
  414.                     } else {
  415.                         SetPort(whichWindow);
  416.                         where = event.where;
  417.                         GlobalToLocal(&where);
  418.                         if (HelpMode) {
  419.                             if (whichObj->help) (*whichObj->help)(where);
  420.                         } else {
  421.                             if (whichObj->click) 
  422.                                 (*whichObj->click)(where, event.modifiers);
  423.                         };
  424.                     };
  425.                     break;
  426.                     
  427.             };
  428.             
  429.             misc_SetCursor();
  430.             
  431.             break;
  432.             
  433.         case keyDown:
  434.         case autoKey:
  435.                 
  436.             SetPort(Front);
  437.             key = event.message & charCodeMask;
  438.             if ((event.modifiers & cmdKey) && key != upArrow &&
  439.                 key != downArrow) {
  440.                 if (key == '?' || key == '/') {
  441.                     HelpMode = true;
  442.                     misc_SetCursor();
  443.                     misc_DisableBB();
  444.                 } else if (key == '.' && HelpMode) {
  445.                     HelpMode = false;
  446.                     misc_SetCursor();
  447.                 } else if (key == '.' && Scanning) {
  448.                     Scanning = FloppyWait = false;
  449.                     Canceled = true;
  450.                     misc_SetCursor();
  451.                 } else {
  452.                     MenuPick = false;
  453.                     Command(MenuKey(event.message & charCodeMask));
  454.                 };
  455.             } else {
  456.                 if (FrontObj->key) (*FrontObj->key)(key, event.modifiers);
  457.             };
  458.             break;
  459.             
  460.         case updateEvt:
  461.         
  462.             whichWindow = (WindowPtr)event.message;
  463.             whichObj = (WindowObject*)((WindowPeek)whichWindow)->refCon;
  464.             SetPort(whichWindow);
  465.             BeginUpdate(whichWindow);
  466.             EraseRect(&whichWindow->portRect);
  467.             if (whichObj->update) (*whichObj->update)();
  468.             EndUpdate(whichWindow);
  469.             break;
  470.             
  471.         case activateEvt:
  472.         
  473.             whichWindow = (WindowPtr)event.message;
  474.             whichObj = (WindowObject*)((WindowPeek)whichWindow)->refCon;
  475.             SetPort(whichWindow);
  476.             if (event.modifiers & activeFlag) {
  477.                 if (whichObj->activate) (*whichObj->activate)();
  478.             } else {
  479.                 if (whichObj->deactivate) (*whichObj->deactivate)();
  480.             };
  481.             break;
  482.             
  483.         case diskEvt:
  484.         
  485.             main_Disk(event.message);
  486.             break;
  487.             
  488.         case osEvt:
  489.         
  490.             if (((event.message >> 24) & 0xff) != suspendResumeMessage) break;
  491.             InForeground = event.message & 1;
  492.             if (FrontKind != daWind) {
  493.                 SetPort(Front);
  494.                 if (InForeground) {
  495.                     if (FrontObj->activate) (*FrontObj->activate)();
  496.                 } else {
  497.                     if (FrontObj->deactivate) (*FrontObj->deactivate)();
  498.                 };
  499.             };
  500.             if (InForeground) {
  501.                 misc_SetCursor();
  502.                 if (Notified) {
  503.                     Notified = false;
  504.                     if (Prefs.notifOption != notifAlert) 
  505.                         NMRemove((QElemPtr)&NotifRec);
  506.                 };
  507.             } else {
  508.                 InitCursor();
  509.             };
  510.             break;
  511.             
  512.     };
  513.     
  514.     /* Restore the saved grafPort. */
  515.     
  516.     SetPort(savedPort);
  517.  
  518. };
  519.     
  520. /*______________________________________________________________________
  521.  
  522.     Main - The Main Program.
  523. _____________________________________________________________________*/
  524.  
  525.  
  526. void main(void)
  527.  
  528. {
  529.     Str255            msg;                /* message to plug in restart alert */
  530.     
  531.     /* Initialize. */
  532.  
  533.     UnloadSeg(_DataInit);
  534.     init_InitMem();
  535.     init_Initialize();
  536.     UnloadSeg(init_Initialize);
  537.     
  538.     /* If scanning station, start scan. */
  539.     
  540.     if (Prefs.scanningStation) vscn_DoScan(autoScan, Prefs.scanningStationOp);
  541.  
  542.     /*    Main event loop. */
  543.     
  544.     while (true) {
  545.     
  546.         prog_Event();
  547.         
  548.         if (Done) {
  549.             if (RepInfected) {
  550.                 switch (utl_StopAlert(saveRepID1, nil, 3)) {
  551.                     case 1: /* Yes */
  552.                         Done = main_Save();
  553.                         break;
  554.                     case 2: /* No */
  555.                         break;
  556.                     case 3: /* Cancel */
  557.                         Done = false;
  558.                         break;
  559.                 };
  560.             };
  561.             if (Done && SysInfect) {
  562.                 GetIndString(msg, strListID, 
  563.                     utl_VolIsMFS(utl_GetSysVol()) ? oldRestartStr : restartStr);
  564.                 ParamText(msg, nil, nil, nil);
  565.                 if (SysHasShutDown) {
  566.                     switch (utl_StopAlert(restartID, nil, 2)) {
  567.                         case 1: /* Restart */
  568.                             misc_WritePref();
  569.                             ShutDwnStart();
  570.                             break;
  571.                         case 2: /* Cancel */
  572.                             Done = false;
  573.                             break;
  574.                         case 3: /* Quit */
  575.                             break;
  576.                     };
  577.                 } else {
  578.                     switch (utl_StopAlert(oldRestartID, nil, 1)) {
  579.                         case 1: /* Cancel */
  580.                             Done = false;
  581.                             break;
  582.                         case 2: /* Quit */
  583.                             break;
  584.                     };
  585.                 }
  586.             };
  587.             if (Done) break;
  588.         };
  589.         
  590.     };
  591.     
  592.     /*    Terminate. */
  593.     
  594.     misc_WritePref();
  595. }
  596.